package com.shao.compress;

import com.shao.compress.impl.GzipCompress;

import java.util.concurrent.ConcurrentHashMap;

/**
 * @author PaoFu
 * @version 1.0
 * @data 2025/4/28 14:41
 */
public class CompressorFactory {

    private static final ConcurrentHashMap<String, CompressorWrapper> COMPRESSOR_CACHE = new ConcurrentHashMap<>(8);
    private static final ConcurrentHashMap<Byte, CompressorWrapper> COMPRESSOR_TYPE = new ConcurrentHashMap<>(8);

    static {
        CompressorWrapper gzip = new CompressorWrapper((byte) 1, "gzip", new GzipCompress());
        COMPRESSOR_CACHE.put("gzip", gzip);

        COMPRESSOR_TYPE.put((byte) 1, gzip);
    }

    /**
     * 使用工厂方法获取CompressorWrapper
     * 没有匹配到兼容的则默认gzip
     * @param compressType 压缩类型
     * @return 返回CompressorWrapper
     */
    public static CompressorWrapper getCompressor(String compressType) {
        return COMPRESSOR_CACHE.get(compressType.toLowerCase()) != null
                ? COMPRESSOR_CACHE.get(compressType)
                : COMPRESSOR_CACHE.get("gzip");
    }

    /**
     * 使用工厂方法获取CompressorWrapper
     * 没有匹配到兼容的则默认gzip
     * @param compressCode 压缩状态码
     * @return 返回CompressorWrapper
     */
    public static CompressorWrapper getCompressor(byte compressCode) {
        return COMPRESSOR_TYPE.get(compressCode) != null
                ? COMPRESSOR_TYPE.get(compressCode)
                : COMPRESSOR_TYPE.get((byte) 1);
    }
}
